﻿using System.Web.Security;
using System.Web.Services;
using System.Web.Services.Protocols;


/// <summary>
/// 关于权限验证的强制性使用，在客户端请求的必需的服务中，加入以下SoapHeader类型的信息的验证
/// </summary>
public class AuthHeaderGB : SoapHeader
{
    public string Username;
    public string Password;

    internal bool AuthenticateUser(string username, string password)
    {
        if ((username != null && password != null) && (Membership.ValidateUser(username, password)))
        {
            return true;
        }
        return false;
    }

    internal bool AuthorizeUser(string username, string[] validRoles)
    {
        string[] rs = Roles.GetRolesForUser(username);
        foreach (string str in rs)
        {
            foreach (string str1 in validRoles)
                if (str == str1)
                {
                    return true;
                }
        }
        return false;
    }
}


/// <summary>
///MembershipService 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[SoapDocumentService(RoutingStyle = SoapServiceRoutingStyle.RequestElement)]
//若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class MembershipService : System.Web.Services.WebService
{
    public AuthHeaderGB sHeader;

    public MembershipService()
    {

        //如果使用设计的组件，请取消注释以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld()
    {
        return "Hello World";
    }


    [WebMethod(Description = "用户账号验证，所有客户端必须调用的方法")]
    [SoapHeader("sHeader")]   //方法要求标头    
    public bool AuthenticateUser()
    {
        return sHeader.AuthenticateUser(sHeader.Username,sHeader.Password);
    }

    [WebMethod(Description = "用户权限验证，传入参数为有权限的角色列表数组")]
    [SoapHeader("sHeader")]   //方法要求标头    
    public bool AuthorizeUser(string[] validRoles)
    {
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }







    //[WebMethod(Description = "获取用色的角色列表")]        //给工程模式的紫光交调测试软件调用，暂时未开放
    [SoapHeader("sHeader")]
    public string[] GetRolesbyUsername()
    {
        return Roles.GetRolesForUser(sHeader.Username);
    }

    [WebMethod(Description = "气密录入操作人员权限验证")]
    [SoapHeader("sHeader")]
    public bool AuthorizeUser_ForAirtightnessService()
    {
        string[] validRoles = { "Administrators", "Manager", "Quality Engineer" };
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }

    [WebMethod(Description = "交调、大功率测试数据手动录入操作人员权限验证")]
    [SoapHeader("sHeader")]
    public bool AuthorizeUser_ForPIMandPowerTest_ManualAddService()
    {
        string[] validRoles = { "Administrators", "PIMandPowerTest_AddbyManual" };
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }

    [WebMethod(Description = "产品装箱工序防错客户端中，尾箱数量配置操作人员权限验证")]
    [SoapHeader("sHeader")]
    public bool AuthorizeUser_ForPackintoBox_MantissaConfigService()
    {
        string[] validRoles = { "Administrators", "装箱软件尾数配置角色" };
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }

    [WebMethod(Description = "装箱前关键工序检查客户端软件，操作人员权限验证用")]
    [SoapHeader("sHeader")]
    public bool AuthorizeUser_ForProductPackage_CheckandPackage()
    {
        string[] validRoles = { "Administrators", "Packer" };
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }

    [WebMethod(Description = "诺基亚车间外箱标签打印客户端软件，操作人员权限验证用")]
    [SoapHeader("sHeader")]
    public bool AuthorizeUser_ForProductPackage_NSNLabelPrint()
    {
        string[] validRoles = { "Administrators", "Packer" };
        return sHeader.AuthorizeUser(sHeader.Username, validRoles);
    }
}

